package cn.net.awit.API.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;

public class StrToJSONUtil {

	public static List<Map<String, Object>> getList(String json){
		List<Object> parseArray = JSONArray.parseArray(json);
		List<Map<String, Object>> list=new ArrayList<>();
		for(Object obj:parseArray){
			Map<String, Object> ret = (Map<String, Object>) obj;
			list.add(ret);
		}
		return list;
	}
	
	/**
	 * 仅用于气象站数据解析
	 * @param json
	 * @return
	 */
	public static Map<String, Object> getMapObj(String json){
		Map<String, Object> mapObj=JSONObject.parseObject(json);
		Object object = mapObj.get("ice");
		if(object!=null){
			String jsonStr=(String) object;
			jsonStr=jsonStr.replace("temp0", "\"temp0\"");
			jsonStr=jsonStr.replace("iceThick0", "\"iceThick0\"");
			jsonStr=jsonStr.replace("alarm0[0]", "\"alarm0[0]\"");
			jsonStr=jsonStr.replace("alarm0[1]", "\"alarm0[1]\"");
			jsonStr=jsonStr.replace("alarm0[2]", "\"alarm0[2]\"");
			jsonStr=jsonStr.replace("temp1", "\"temp1\"");
			jsonStr=jsonStr.replace("iceThick1", "\"iceThick1\"");
			jsonStr=jsonStr.replace("alarm1[0]", "\"alarm1[0]\"");
			jsonStr=jsonStr.replace("alarm1[1]", "\"alarm1[1]\"");
			jsonStr=jsonStr.replace("alarm1[2]", "\"alarm1[2]\"");
			List<Map<String, Object>> arr = (List<Map<String, Object>>) JSONArray.parse(jsonStr);
			mapObj.put("ice", arr);
		}
		return mapObj;
	}
	
	public static Date getTime(String timeStr,String pattern) throws ParseException{
		SimpleDateFormat formatter=new SimpleDateFormat(pattern); 
		Date parse = formatter.parse(timeStr);
		return parse;
	}
	
}
